package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.result.Result;
import com.ruoyi.system.domain.Teacher;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static org.apache.naming.SelectorContext.prefix;

/**
 * <p>
 * 教师表 前端控制器
 * </p>
 *
 * @author FSD
 * @since 2024-02-05
 */
@Api(tags = "教师表")
@RestController
@RequestMapping("/api/system/teacher")
public class TeacherController {
    private String prefix = "/api/system/teacher";
    @Resource(name = "TeacherServiceImpl")
    private ITeacherService teacherService;
    @Resource(name = "SysUserServiceImpl")
    private ISysUserService sysUserService;


    @RequiresPermissions("system:teacher:view")
    @GetMapping()
    public String teacher()
    {
        return prefix + "/teacher";
    }
    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增")
    @ApiImplicitParam(name = "teacher", value = "教师实体类",dataTypeClass = String.class)
    @RequiresRoles("admin")
    public Result insert(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if (save) {
            SysUser sysUser = new SysUser();
            sysUser.setUserName(teacher.getUsername());
            sysUser.setPassword(teacher.getPassword());
            sysUser.setPhonenumber(teacher.getPhone());
            sysUser.setRoleId(3L);
            sysUser.setStatus("0");
            sysUser.setDelFlag("0");
            sysUser.setCreateBy("admin");
            sysUserService.insertUser(sysUser);
        }
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParam(name = "teacher", value = "教师实体类",dataTypeClass = String.class)
    @RequiresRoles("admin")
    public Result update(@RequestBody Teacher teacher) {
        boolean b = teacherService.updateById(teacher);
        if (b) {
            SysUser sysUser = new SysUser();
            sysUser.setUserName(teacher.getUsername());
            sysUser.setPassword(teacher.getPassword());
            sysUser.setPhonenumber(teacher.getPhone());
            sysUser.setRoleId(3L);
            sysUser.setStatus("0");
            sysUser.setDelFlag("0");
            sysUser.setCreateBy("admin");
            sysUserService.updateUser(sysUser);
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParam(name = "id", value = "id",dataTypeClass = String.class)
    @RequiresRoles("admin")
    public Result delete(@PathVariable Integer id) {
        String username = teacherService.getById(id).getUsername();
        if (!username.isEmpty()){
            teacherService.removeById(id);
            Long userId = sysUserService.selectUserByLoginName(username).getUserId();
            sysUserService.deleteUserById(userId);
            return Result.ok();
        }
        return Result.fail();
    }



    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "查询全部")
    @RequiresRoles("admin")
    public Result findAll() {
        return Result.ok(teacherService.list());
    }


    /**
     * 分页模糊查询
     */
    @GetMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "分页模糊查询")
    @RequiresRoles("admin")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(teacherService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

